$(function () {
    selectHeadLink();
})

function selectHeadLink() {
    let data = myAjax('/back/mainPage/selectHeadLink', '', 'get');
    let list = data.list;
    let html = '';
    for (let i = 0; i < list.length; i++) {
        html += '<tr id="show'+i+'">\n' +
            '            <td>' + list[i].id + '</td>\n' +
            '            <td>' + list[i].title + '</td>\n' +
            '            <td>' + list[i].href + '</td>\n' +
            '            <td>' + list[i].isshow+ '</td>\n' +
            '            <td><a href="javascript:add()">新增</a>&nbsp;' +
            '                <a id="isshow'+i+'" href="javascript:updateisshow(' + list[i].id + ','+list[i].isshow+')">禁用</a>&nbsp;' +
            '                <a href="javascript:edit(' + list[i].id + ')">修改</a>' +
            '            </td>\n' +
            '        </tr>';
    }
    $("#findHeadLink").html(html);
    //是否启用OR禁用
    for (let i = 0; i < list.length; i++) {
        if (list[i].isshow==0){
            $("#show"+i+"").css("background-color","palegoldenrod");
            $("#isshow"+i+"").text("启用");
        }
    }
}

//新增信息==================================
function add() {
    let html = '';
    html += '<div style="float: left;margin-right:10px">地址名称：<input id="title" type="text"></div>\n' +
        '    <div style="float: left;margin-right:10px">地址链接：<input id="href" type="text"></div>\n' +
        '    <div style="float: left;margin-right:10px"><button onclick="addHeadLink()">新增</button></div>';
    $("#editHeadLink").html(html);
}

function addHeadLink() {
    let data1 = {
        title: $("#title").val(),
        href: $("#href").val()
    };
    let data = myAjax('/back/mainPage/addHeadLink', data1, 'post');
    if (data.results == 1) {
        alert("添加成功");
        $('#right').load('/back/html/mainPage/headLinkManage.html');
    } else {
        alert("添加失败");
    }
}

//修改信息==================================
function edit(id) {
    let data = myAjax('/back/mainPage/selectHeadLinkById', {id: id}, 'get');
    data = data.model;
    let html = '';
    html += '<div>地址名称：<input id="title" type="text" value=' + data.title + '></div>\n' +
        '    <div>地址链接：<textarea id="href">' + data.href + '</textarea></div>\n' +
        '    <div><button onclick="updateHeadLink(' + data.id + ')">确定修改</button></div>';
    $("#editHeadLink").html(html);
}

//更新HeadandLink的信息
function updateHeadLink(id) {
    let data1 = {
        id: id,
        title: $("#title").val(),
        href: $("#href").val()
    };
    let data = myAjax('/back/mainPage/updateHeadLinkById', data1, 'post');
    if (data.result == 1) {
        alert("更新成功");
        $('#right').load('/back/html/mainPage/headLinkManage.html');
    } else {
        alert("更新失败");
    }
}
//启用////禁用
function updateisshow(id,isshow) {
    isshow = isshow == 0 ? 1:0;
    let data = myAjax('/back/mainPage/updateIsshowHeadLink', {id: id,isshow:isshow}, 'post');
    let i = id-1;
    if (isshow == 0){
        $("#show"+i+"").css("background-color","");
        $("#isshow"+i+"").text("启用");
    }else {
        $("#show"+i+"").css("background-color","palegoldenrod");
        $("#isshow"+i+"").text("禁用");
    }
    selectHeadLink();
}